#ifndef __M_SERVER_H__
#define __M_SERVER_H__

#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <cstring>
#include <ctime>
#include <functional>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <typeinfo>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>

#define INF 0
#define DBG 1
#define ERR 2
#define LOG_LEVEL DBG

#define LOG(level, format, ...) do{\
        if (level < LOG_LEVEL) break;\
        time_t t = time(NULL);\
        struct tm *ltm = localtime(&t);\
        char tmp[32] = {0};\
        strftime(tmp, 31, "%H:%M:%S", ltm);\
        fprintf(stdout, "[%p %s %s:%d] " format "\n", (void*)pthread_self(), tmp, __FILE__, __LINE__, ##__VA_ARGS__);\
    }while(0)

#define INF_LOG(format, ...) LOG(INF, format, ##__VA_ARGS__)
#define DBG_LOG(format, ...) LOG(DBG, format, ##__VA_ARGS__)
#define ERR_LOG(format, ...) LOG(ERR, format, ##__VA_ARGS__)

#define BUFFER_DEFAULT_SIZE 1024

// Buffer 类，用于管理数据缓冲区
class Buffer {
    private:
        std::vector<char> _buffer; // 数据缓冲区
        uint64_t _reader_idx; // 读偏移
        uint64_t _writer_idx; // 写偏移
    public:
        Buffer():_reader_idx(0), _writer_idx(0), _buffer(BUFFER_DEFAULT_SIZE){}
        
        // 获取缓冲区起始位置指针
        char *Begin() { return &*_buffer.begin(); }
        
        // 获取写入位置指针
        char *WritePosition() { return Begin() + _writer_idx; }
        
        // 获取读取位置指针
        char *ReadPosition() { return Begin() + _reader_idx; }
        
        // 获取尾部空闲大小
        uint64_t TailIdleSize() { return _buffer.size() - _writer_idx; }
        
        // 获取头部空闲大小
        uint64_t HeadIdleSize() { return _reader_idx; }
        
        // 获取可读数据大小
        uint64_t ReadAbleSize() { return _writer_idx - _reader_idx; }
        
        // 移动读偏移
        void MoveReadOffset(uint64_t len) { 
            if (len == 0) return; 
            assert(len <= ReadAbleSize());
            _reader_idx += len;
        }
        
        // 移动写偏移
        void MoveWriteOffset(uint64_t len) {
            assert(len <= TailIdleSize());
            _writer_idx += len;
        }
        
        // 确保有足够的写空间
        void EnsureWriteSpace(uint64_t len) {
            if (TailIdleSize() >= len) { return; }
            if (len <= TailIdleSize() + HeadIdleSize()) {
                uint64_t rsz = ReadAbleSize();
                std::copy(ReadPosition(), ReadPosition() + rsz, Begin());
                _reader_idx = 0;    
                _writer_idx = rsz;
            } else {
                DBG_LOG("RESIZE %ld", _writer_idx + len);
                _buffer.resize(_writer_idx + len);
            }
        }
        
        // 写入数据
        void Write(const void *data, uint64_t len) {
            if (len == 0) return;
            EnsureWriteSpace(len);
            const char *d = (const char *)data;
            std::copy(d, d + len, WritePosition());
        }
        
        // 写入并推进写偏移
        void WriteAndPush(const void *data, uint64_t len) {
            Write(data, len);
            MoveWriteOffset(len);
        }
        
        // 写入字符串
        void WriteString(const std::string &data) {
            return Write(data.c_str(), data.size());
        }
        
        // 写入字符串并推进写偏移
        void WriteStringAndPush(const std::string &data) {
            WriteString(data);
            MoveWriteOffset(data.size());
        }
        
        // 写入另一个缓冲区的数据
        void WriteBuffer(Buffer &data) {
            return Write(data.ReadPosition(), data.ReadAbleSize());
        }
        
        // 写入另一个缓冲区的数据并推进写偏移
        void WriteBufferAndPush(Buffer &data) { 
            WriteBuffer(data);
            MoveWriteOffset(data.ReadAbleSize());
        }
        
        // 读取数据
        void Read(void *buf, uint64_t len) {
            assert(len <= ReadAbleSize());
            std::copy(ReadPosition(), ReadPosition() + len, (char*)buf);
        }
        
        // 读取并推进读偏移
        void ReadAndPop(void *buf, uint64_t len) {
            Read(buf, len);
            MoveReadOffset(len);
        }
        
        // 读取为字符串
        std::string ReadAsString(uint64_t len) {
            assert(len <= ReadAbleSize());
            std::string str;
            str.resize(len);
            Read(&str[0], len);
            return str;
        }
        
        // 读取为字符串并推进读偏移
        std::string ReadAsStringAndPop(uint64_t len) {
            assert(len <= ReadAbleSize());
            std::string str = ReadAsString(len);
            MoveReadOffset(len);
            return str;
        }
        
        // 查找换行符
        char *FindCRLF() {
            char *res = (char*)memchr(ReadPosition(), '\n', ReadAbleSize());
            return res;
        }
        
        // 获取一行数据
        std::string GetLine() {
            char *pos = FindCRLF();
            if (pos == NULL) {
                return "";
            }
            return ReadAsString(pos - ReadPosition() + 1);
        }
        
        // 获取一行数据并推进读偏移
        std::string GetLineAndPop() {
            std::string str = GetLine();
            MoveReadOffset(str.size());
            return str;
        }
        
        // 清空缓冲区
        void Clear() {
            _reader_idx = 0;
            _writer_idx = 0;
        }
};

// Socket 类，用于管理套接字
#define MAX_LISTEN 1024
class Socket {
    private:
        int _sockfd; // 套接字文件描述符
    public:
        Socket():_sockfd(-1) {}
        Socket(int fd): _sockfd(fd) {}
        ~Socket() { Close(); }
        
        // 获取文件描述符
        int Fd() { return _sockfd; }
        
        // 创建套接字
        bool Create() {
            _sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            if (_sockfd < 0) {
                ERR_LOG("CREATE SOCKET FAILED!!");
                return false;
            }
            return true;
        }
        
        // 绑定地址信息
        bool Bind(const std::string &ip, uint16_t port) {
            struct sockaddr_in addr;
            addr.sin_family = AF_INET;
            addr.sin_port = htons(port);
            addr.sin_addr.s_addr = inet_addr(ip.c_str());
            socklen_t len = sizeof(struct sockaddr_in);
            int ret = bind(_sockfd, (struct sockaddr*)&addr, len);
            if (ret < 0) {
                ERR_LOG("BIND ADDRESS FAILED!");
                return false;
            }
            return true;
        }
        
        // 开始监听
        bool Listen(int backlog = MAX_LISTEN) {
            int ret = listen(_sockfd, backlog);
            if (ret < 0) {
                ERR_LOG("SOCKET LISTEN FAILED!");
                return false;
            }
            return true;
        }
        
        // 连接服务器
        bool Connect(const std::string &ip, uint16_t port) {
            struct sockaddr_in addr;
            addr.sin_family = AF_INET;
            addr.sin_port = htons(port);
            addr.sin_addr.s_addr = inet_addr(ip.c_str());
            socklen_t len = sizeof(struct sockaddr_in);
            int ret = connect(_sockfd, (struct sockaddr*)&addr, len);
            if (ret < 0) {
                ERR_LOG("CONNECT SERVER FAILED!");
                return false;
            }
            return true;
        }
        
        // 接受新连接
        int Accept() {
            int newfd = accept(_sockfd, NULL, NULL);
            if (newfd < 0) {
                ERR_LOG("SOCKET ACCEPT FAILED!");
                return -1;
            }
            return newfd;
        }
        
        // 接收数据
        ssize_t Recv(void *buf, size_t len, int flag = 0) {
            ssize_t ret = recv(_sockfd, buf, len, flag);
            if (ret <= 0) {
                if (errno == EAGAIN || errno == EINTR) {
                    return 0;
                }
                ERR_LOG("SOCKET RECV FAILED!!");
                return -1;
            }
            return ret;
        }
        
        // 非阻塞接收数据
        ssize_t NonBlockRecv(void *buf, size_t len) {
            return Recv(buf, len, MSG_DONTWAIT);
        }
        
        // 发送数据
        ssize_t Send(const void *buf, size_t len, int flag = 0) {
            ssize_t ret = send(_sockfd, buf, len, flag);
            if (ret < 0) {
                if (errno == EAGAIN || errno == EINTR) {
                    return 0;
                }
                ERR_LOG("SOCKET SEND FAILED!!");
                return -1;
            }
            return ret;
        }
        
        // 非阻塞发送数据
        ssize_t NonBlockSend(void *buf, size_t len) {
            if (len == 0) return 0;
            return Send(buf, len, MSG_DONTWAIT);
        }
        
        // 关闭套接字
        void Close() {
            if (_sockfd != -1) {
                close(_sockfd);
                _sockfd = -1;
            }
        }
        
        // 创建一个服务端连接
        bool CreateServer(uint16_t port, const std::string &ip = "0.0.0.0", bool block_flag = false) {
            if (Create() == false) return false;
            if (block_flag) NonBlock();
            if (Bind(ip, port) == false) return false;
            if (Listen() == false) return false;
            ReuseAddress();
            return true;
        }
        
        // 创建一个客户端连接
        bool CreateClient(uint16_t port, const std::string &ip) {
            if (Create() == false) return false;
            if (Connect(ip, port) == false) return false;
            return true;
        }
        
        // 设置套接字选项-开启地址端口重用
        void ReuseAddress() {
            int val = 1;
            setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&val, sizeof(int));
            val = 1;
            setsockopt(_sockfd, SOL_SOCKET, SO_REUSEPORT, (void*)&val, sizeof(int));
        }
        
        // 设置套接字为非阻塞
        void NonBlock() {
            int flag = fcntl(_sockfd, F_GETFL, 0);
            fcntl(_sockfd, F_SETFL, flag | O_NONBLOCK);
        }
};

// Poller 类，用于管理 epoll 事件
class Poller;
class EventLoop;

// Channel 类，用于管理单个文件描述符的事件
class Channel {
    private:
        int _fd; // 文件描述符
        EventLoop *_loop; // 关联的 EventLoop
        uint32_t _events; // 需要监控的事件
        uint32_t _revents; // 实际触发的事件
        using EventCallback = std::function<void()>;
        EventCallback _read_callback; // 可读事件回调
        EventCallback _write_callback; // 可写事件回调
        EventCallback _error_callback; // 错误事件回调
        EventCallback _close_callback; // 关闭事件回调
        EventCallback _event_callback; // 任意事件回调
    public:
        Channel(EventLoop *loop, int fd):_fd(fd), _events(0), _revents(0), _loop(loop) {}
        int Fd() { return _fd; }
        uint32_t Events() { return _events; } // 获取需要监控的事件
        void SetREvents(uint32_t events) { _revents = events; } // 设置实际触发的事件
        void SetReadCallback(const EventCallback &cb) { _read_callback = cb; }
        void SetWriteCallback(const EventCallback &cb) { _write_callback = cb; }
        void SetErrorCallback(const EventCallback &cb) { _error_callback = cb; }
        void SetCloseCallback(const EventCallback &cb) { _close_callback = cb; }
        void SetEventCallback(const EventCallback &cb) { _event_callback = cb; }
        bool ReadAble() { return (_events & EPOLLIN); } // 判断是否监控了可读事件
        bool WriteAble() { return (_events & EPOLLOUT); } // 判断是否监控了可写事件
        void EnableRead() { _events |= EPOLLIN; Update(); } // 启动读事件监控
        void EnableWrite() { _events |= EPOLLOUT; Update(); } // 启动写事件监控
        void DisableRead() { _events &= ~EPOLLIN; Update(); } // 关闭读事件监控
        void DisableWrite() { _events &= ~EPOLLOUT; Update(); } // 关闭写事件监控
        void DisableAll() { _events = 0; Update(); } // 关闭所有事件监控
        void Remove();
        void Update();
        void HandleEvent() {
            if ((_revents & EPOLLIN) || (_revents & EPOLLRDHUP) || (_revents & EPOLLPRI)) {
                if (_read_callback) _read_callback();
            }
            if (_revents & EPOLLOUT) {
                if (_write_callback) _write_callback();
            } else if (_revents & EPOLLERR) {
                if (_error_callback) _error_callback();
            } else if (_revents & EPOLLHUP) {
                if (_close_callback) _close_callback();
            }
            if (_event_callback) _event_callback();
        }
};

#define MAX_EPOLLEVENTS 1024

// Poller 类，用于管理 epoll 事件
class Poller {
    private:
        int _epfd; // epoll 文件描述符
        struct epoll_event _evs[MAX_EPOLLEVENTS]; // epoll 事件数组
        std::unordered_map<int, Channel *> _channels; // 文件描述符到 Channel 的映射
    private:
        void Update(Channel *channel, int op) {
            int fd = channel->Fd();
            struct epoll_event ev;
            ev.data.fd = fd;
            ev.events = channel->Events();
            int ret = epoll_ctl(_epfd, op, fd, &ev);
            if (ret < 0) {
                ERR_LOG("EPOLLCTL FAILED!");
            }
            return;
        }
        bool HasChannel(Channel *channel) {
            auto it = _channels.find(channel->Fd());
            if (it == _channels.end()) {
                return false;
            }
            return true;
        }
    public:
        Poller() {
            _epfd = epoll_create(MAX_EPOLLEVENTS);
            if (_epfd < 0) {
                ERR_LOG("EPOLL CREATE FAILED!!");
                abort();
            }
        }
        void UpdateEvent(Channel *channel) {
            bool ret = HasChannel(channel);
            if (ret == false) {
                _channels.insert(std::make_pair(channel->Fd(), channel));
                return Update(channel, EPOLL_CTL_ADD);
            }
            return Update(channel, EPOLL_CTL_MOD);
        }
        void RemoveEvent(Channel *channel) {
            auto it = _channels.find(channel->Fd());
            if (it != _channels.end()) {
                _channels.erase(it);
            }
            Update(channel, EPOLL_CTL_DEL);
        }
        void Poll(std::vector<Channel*> *active) {
            int nfds = epoll_wait(_epfd, _evs, MAX_EPOLLEVENTS, -1);
            if (nfds < 0) {
                if (errno == EINTR) {
                    return ;
                }
                ERR_LOG("EPOLL WAIT ERROR:%s\n", strerror(errno));
                abort();
            }
            for (int i = 0; i < nfds; i++) {
                auto it = _channels.find(_evs[i].data.fd);
                assert(it != _channels.end());
                it->second->SetREvents(_evs[i].events);
                active->push_back(it->second);
            }
            return;
        }
};

using TaskFunc = std::function<void()>;
using ReleaseFunc = std::function<void()>;

// TimerTask 类，用于管理定时任务
class TimerTask {
    private:
        uint64_t _id; // 定时任务 ID
        uint32_t _timeout; // 超时时间
        bool _canceled; // 是否取消
        TaskFunc _task_cb; // 定时任务回调函数
        ReleaseFunc _release; // 释放回调函数
    public:
        TimerTask(uint64_t id, uint32_t delay, const TaskFunc &cb): 
            _id(id), _timeout(delay), _task_cb(cb), _canceled(false) {}
        ~TimerTask() { 
            if (_canceled == false) _task_cb(); 
            _release(); 
        }
        void Cancel() { _canceled = true; } // 取消定时任务
        void SetRelease(const ReleaseFunc &cb) { _release = cb; } // 设置释放回调
        uint32_t DelayTime() { return _timeout; } // 获取超时时间
};

// TimerWheel 类，用于管理定时任务轮
class TimerWheel {
    private:
        using WeakTask = std::weak_ptr<TimerTask>;
        using PtrTask = std::shared_ptr<TimerTask>;
        int _tick; // 当前秒针位置
        int _capacity; // 表盘容量
        std::vector<std::vector<PtrTask>> _wheel; // 定时任务轮
        std::unordered_map<uint64_t, WeakTask> _timers; // 定时任务映射

        EventLoop *_loop; // 关联的 EventLoop
        int _timerfd; // 定时器文件描述符
        std::unique_ptr<Channel> _timer_channel; // 定时器 Channel
    private:
        void RemoveTimer(uint64_t id) {
            auto it = _timers.find(id);
            if (it != _timers.end()) {
                _timers.erase(it);
            }
        }
        static int CreateTimerfd() {
            int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
            if (timerfd < 0) {
                ERR_LOG("TIMERFD CREATE FAILED!");
                abort();
            }
            struct itimerspec itime;
            itime.it_value.tv_sec = 1;
            itime.it_value.tv_nsec = 0;
            itime.it_interval.tv_sec = 1; 
            itime.it_interval.tv_nsec = 0;
            timerfd_settime(timerfd, 0, &itime, NULL);
            return timerfd;
        }
        int ReadTimefd() {
            uint64_t times;
            int ret = read(_timerfd, &times, 8);
            if (ret < 0) {
                ERR_LOG("READ TIMEFD FAILED!");
                abort();
            }
            return times;
        }
        void RunTimerTask() {
            _tick = (_tick + 1) % _capacity;
            _wheel[_tick].clear();
        }
        void OnTime() {
            int times = ReadTimefd();
            for (int i = 0; i < times; i++) {
                RunTimerTask();
            }
        }
        void TimerAddInLoop(uint64_t id, uint32_t delay, const TaskFunc &cb) {
            PtrTask pt(new TimerTask(id, delay, cb));
            pt->SetRelease(std::bind(&TimerWheel::RemoveTimer, this, id));
            int pos = (_tick + delay) % _capacity;
            _wheel[pos].push_back(pt);
            _timers[id] = WeakTask(pt);
        }
        void TimerRefreshInLoop(uint64_t id) {
            auto it = _timers.find(id);
            if (it == _timers.end()) {
                return;
            }
            PtrTask pt = it->second.lock();
            int delay = pt->DelayTime();
            int pos = (_tick + delay) % _capacity;
            _wheel[pos].push_back(pt);
        }
        void TimerCancelInLoop(uint64_t id) {
            auto it = _timers.find(id);
            if (it == _timers.end()) {
                return;
            }
            PtrTask pt = it->second.lock();
            if (pt) pt->Cancel();
        }
    public:
        TimerWheel(EventLoop *loop):_capacity(60), _tick(0), _wheel(_capacity), _loop(loop), 
            _timerfd(CreateTimerfd()), _timer_channel(new Channel(_loop, _timerfd)) {
            _timer_channel->SetReadCallback(std::bind(&TimerWheel::OnTime, this));
            _timer_channel->EnableRead();
        }
        void TimerAdd(uint64_t id, uint32_t delay, const TaskFunc &cb);
        void TimerRefresh(uint64_t id);
        void TimerCancel(uint64_t id);
        bool HasTimer(uint64_t id) {
            auto it = _timers.find(id);
            if (it == _timers.end()) {
                return false;
            }
            return true;
        }
};

// EventLoop 类，用于管理事件循环
class EventLoop {
    private:
        using Functor = std::function<void()>;
        std::thread::id _thread_id; // 线程 ID
        int _event_fd; // eventfd 描述符
        std::unique_ptr<Channel> _event_channel; // eventfd 的 Channel
        Poller _poller; // epoll 事件管理器
        std::vector<Functor> _tasks; // 任务池
        std::mutex _mutex; // 任务池操作的互斥锁
        TimerWheel _timer_wheel; // 定时任务轮
    public:
        void RunAllTask() {
            std::vector<Functor> functor;
            {
                std::unique_lock<std::mutex> _lock(_mutex);
                _tasks.swap(functor);
            }
            for (auto &f : functor) {
                f();
            }
            return ;
        }
        static int CreateEventFd() {
            int efd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
            if (efd < 0) {
                ERR_LOG("CREATE EVENTFD FAILED!!");
                abort();
            }
            return efd;
        }
        void ReadEventfd() {
            uint64_t res = 0;
            int ret = read(_event_fd, &res, sizeof(res));
            if (ret < 0) {
                if (errno == EINTR || errno == EAGAIN) {
                    return;
                }
                ERR_LOG("READ EVENTFD FAILED!");
                abort();
            }
            return ;
        }
        void WeakUpEventFd() {
            uint64_t val = 1;
            int ret = write(_event_fd, &val, sizeof(val));
            if (ret < 0) {
                if (errno == EINTR) {
                    return;
                }
                ERR_LOG("READ EVENTFD FAILED!");
                abort();
            }
            return ;
        }
    public:
        EventLoop():_thread_id(std::this_thread::get_id()), 
                    _event_fd(CreateEventFd()), 
                    _event_channel(new Channel(this, _event_fd)),
                    _timer_wheel(this) {
            _event_channel->SetReadCallback(std::bind(&EventLoop::ReadEventfd, this));
            _event_channel->EnableRead();
        }
        void Start() {
            while(1) {
                std::vector<Channel *> actives;
                _poller.Poll(&actives);
                for (auto &channel : actives) {
                    channel->HandleEvent();
                }
                RunAllTask();
            }
        }
        bool IsInLoop() {
            return (_thread_id == std::this_thread::get_id());
        }
        void AssertInLoop() {
            assert(_thread_id == std::this_thread::get_id());
        }
        void RunInLoop(const Functor &cb) {
            if (IsInLoop()) {
                return cb();
            }
            return QueueInLoop(cb);
        }
        void QueueInLoop(const Functor &cb) {
            {
                std::unique_lock<std::mutex> _lock(_mutex);
                _tasks.push_back(cb);
            }
            WeakUpEventFd();
        }
        void UpdateEvent(Channel *channel) { return _poller.UpdateEvent(channel); }
        void RemoveEvent(Channel *channel) { return _poller.RemoveEvent(channel); }
        void TimerAdd(uint64_t id, uint32_t delay, const TaskFunc &cb) { return _timer_wheel.TimerAdd(id, delay, cb); }
        void TimerRefresh(uint64_t id) { return _timer_wheel.TimerRefresh(id); }
        void TimerCancel(uint64_t id) { return _timer_wheel.TimerCancel(id); }
        bool HasTimer(uint64_t id) { return _timer_wheel.HasTimer(id); }
};

// LoopThread 类，用于管理单个事件循环线程
class LoopThread {
    private:
        std::mutex _mutex; // 互斥锁
        std::condition_variable _cond; // 条件变量
        EventLoop *_loop; // EventLoop 对象
        std::thread _thread; // 线程对象
    private:
        void ThreadEntry() {
            EventLoop loop;
            {
                std::unique_lock<std::mutex> lock(_mutex);
                _loop = &loop;
                _cond.notify_all();
            }
            loop.Start();
        }
    public:
        LoopThread():_loop(NULL), _thread(std::thread(&LoopThread::ThreadEntry, this)) {}
        EventLoop *GetLoop() {
            EventLoop *loop = NULL;
            {
                std::unique_lock<std::mutex> lock(_mutex);
                _cond.wait(lock, [&](){ return _loop != NULL; });
                loop = _loop;
            }
            return loop;
        }
};

// LoopThreadPool 类，用于管理事件循环线程池
class LoopThreadPool {
    private:
        int _thread_count; // 线程数量
        int _next_idx; // 下一个线程索引
        EventLoop *_baseloop; // 基础 EventLoop
        std::vector<LoopThread*> _threads; // 线程数组
        std::vector<EventLoop *> _loops; // EventLoop 数组
    public:
        LoopThreadPool(EventLoop *baseloop):_thread_count(0), _next_idx(0), _baseloop(baseloop) {}
        void SetThreadCount(int count) { _thread_count = count; }
        void Create() {
            if (_thread_count > 0) {
                _threads.resize(_thread_count);
                _loops.resize(_thread_count);
                for (int i = 0; i < _thread_count; i++) {
                    _threads[i] = new LoopThread();
                    _loops[i] = _threads[i]->GetLoop();
                }
            }
            return ;
        }
        EventLoop *NextLoop() {
            if (_thread_count == 0) {
                return _baseloop;
            }
            _next_idx = (_next_idx + 1) % _thread_count;
            return _loops[_next_idx];
        }
};

// Any 类，用于保存任意类型的数据
class Any {
    private:
        class holder {
            public:
                virtual ~holder() {}
                virtual const std::type_info& type() = 0;
                virtual holder *clone() = 0;
        };
        template<class T>
        class placeholder: public holder {
            public:
                placeholder(const T &val): _val(val) {}
                virtual const std::type_info& type() { return typeid(T); }
                virtual holder *clone() { return new placeholder(_val); }
            public:
                T _val;
        };
        holder *_content;
    public:
        Any():_content(NULL) {}
        template<class T>
        Any(const T &val):_content(new placeholder<T>(val)) {}
        Any(const Any &other):_content(other._content ? other._content->clone() : NULL) {}
        ~Any() { delete _content; }

        Any &swap(Any &other) {
            std::swap(_content, other._content);
            return *this;
        }

        template<class T>
        T *get() {
            assert(typeid(T) == _content->type());
            return &((placeholder<T>*)_content)->_val;
        }
        template<class T>
        Any& operator=(const T &val) {
            Any(val).swap(*this);
            return *this;
        }
        Any& operator=(const Any &other) {
            Any(other).swap(*this);
            return *this;
        }
};

// Connection 类，用于管理单个连接
typedef enum { DISCONNECTED, CONNECTING, CONNECTED, DISCONNECTING} ConnStatu;
using PtrConnection = std::shared_ptr<Connection>;

class Connection : public std::enable_shared_from_this<Connection> {
    private:
        uint64_t _conn_id; // 连接 ID
        int _sockfd; // 套接字文件描述符
        bool _enable_inactive_release; // 是否启用非活跃释放
        EventLoop *_loop; // 关联的 EventLoop
        ConnStatu _statu; // 连接状态
        Socket _socket; // 套接字
        Channel _channel; // 事件 Channel
        Buffer _in_buffer; // 输入缓冲区
        Buffer _out_buffer; // 输出缓冲区
        Any _context; // 上下文

        using ConnectedCallback = std::function<void(const PtrConnection&)>;
        using MessageCallback = std::function<void(const PtrConnection&, Buffer *)>;
        using ClosedCallback = std::function<void(const PtrConnection&)>;
        using AnyEventCallback = std::function<void(const PtrConnection&)>;
        ConnectedCallback _connected_callback; // 连接成功回调
        MessageCallback _message_callback; // 消息回调
        ClosedCallback _closed_callback; // 关闭回调
        AnyEventCallback _event_callback; // 任意事件回调
        ClosedCallback _server_closed_callback; // 服务器关闭回调
    private:
        void HandleRead() {
            char buf[65536];
            ssize_t ret = _socket.NonBlockRecv(buf, 65535);
            if (ret < 0) {
                return ShutdownInLoop();
            }
            _in_buffer.WriteAndPush(buf, ret);
            if (_in_buffer.ReadAbleSize() > 0) {
                return _message_callback(shared_from_this(), &_in_buffer);
            }
        }
        void HandleWrite() {
            ssize_t ret = _socket.NonBlockSend(_out_buffer.ReadPosition(), _out_buffer.ReadAbleSize());
            if (ret < 0) {
                if (_in_buffer.ReadAbleSize() > 0) {
                    _message_callback(shared_from_this(), &_in_buffer);
                }
                return Release();
            }
            _out_buffer.MoveReadOffset(ret);
            if (_out_buffer.ReadAbleSize() == 0) {
                _channel.DisableWrite();
                if (_statu == DISCONNECTING) {
                    return Release();
                }
            }
            return;
        }
        void HandleClose() {
            if (_in_buffer.ReadAbleSize() > 0) {
                _message_callback(shared_from_this(), &_in_buffer);
            }
            return Release();
        }
        void HandleError() {
            return HandleClose();
        }
        void HandleEvent() {
            if (_enable_inactive_release == true)  {  _loop->TimerRefresh(_conn_id); }
            if (_event_callback)  {  _event_callback(shared_from_this()); }
        }
        void EstablishedInLoop() {
            assert(_statu == CONNECTING);
            _statu = CONNECTED;
            _channel.EnableRead();
            if (_connected_callback) _connected_callback(shared_from_this());
        }
        void ReleaseInLoop() {
            _statu = DISCONNECTED;
            _channel.Remove();
            _socket.Close();
            if (_loop->HasTimer(_conn_id)) CancelInactiveReleaseInLoop();
            if (_closed_callback) _closed_callback(shared_from_this());
            if (_server_closed_callback) _server_closed_callback(shared_from_this());
        }
        void SendInLoop(Buffer &buf) {
            if (_statu == DISCONNECTED) return ;
            _out_buffer.WriteBufferAndPush(buf);
            if (_channel.WriteAble() == false) {
                _channel.EnableWrite();
            }
        }
        void ShutdownInLoop() {
            _statu = DISCONNECTING;
            if (_in_buffer.ReadAbleSize() > 0) {
                if (_message_callback) _message_callback(shared_from_this(), &_in_buffer);
            }
            if (_out_buffer.ReadAbleSize() > 0) {
                if (_channel.WriteAble() == false) {
                    _channel.EnableWrite();
                }
            }
            if (_out_buffer.ReadAbleSize() == 0) {
                Release();
            }
        }
        void EnableInactiveReleaseInLoop(int sec) {
            _enable_inactive_release = true;
            if (_loop->HasTimer(_conn_id)) {
                return _loop->TimerRefresh(_conn_id);
            }
            _loop->TimerAdd(_conn_id, sec, std::bind(&Connection::Release, this));
        }
        void CancelInactiveReleaseInLoop() {
            _enable_inactive_release = false;
            if (_loop->HasTimer(_conn_id)) { 
                _loop->TimerCancel(_conn_id); 
            }
        }
        void UpgradeInLoop(const Any &context, 
                    const ConnectedCallback &conn, 
                    const MessageCallback &msg, 
                    const ClosedCallback &closed, 
                    const AnyEventCallback &event) {
            _context = context;
            _connected_callback = conn;
            _message_callback = msg;
            _closed_callback = closed;
            _event_callback = event;
        }
    public:
        Connection(EventLoop *loop, uint64_t conn_id, int sockfd):_conn_id(conn_id), _sockfd(sockfd),
            _enable_inactive_release(false), _loop(loop), _statu(CONNECTING), _socket(_sockfd),
            _channel(loop, _sockfd) {
            _channel.SetCloseCallback(std::bind(&Connection::HandleClose, this));
            _channel.SetEventCallback(std::bind(&Connection::HandleEvent, this));
            _channel.SetReadCallback(std::bind(&Connection::HandleRead, this));
            _channel.SetWriteCallback(std::bind(&Connection::HandleWrite, this));
            _channel.SetErrorCallback(std::bind(&Connection::HandleError, this));
        }
        ~Connection() { DBG_LOG("RELEASE CONNECTION:%p", this); }
        int Fd() { return _sockfd; }
        int Id() { return _conn_id; }
        bool Connected() { return (_statu == CONNECTED); }
        void SetContext(const Any &context) { _context = context; }
        Any *GetContext() { return &_context; }
        void SetConnectedCallback(const ConnectedCallback&cb) { _connected_callback = cb; }
        void SetMessageCallback(const MessageCallback&cb) { _message_callback = cb; }
        void SetClosedCallback(const ClosedCallback&cb) { _closed_callback = cb; }
        void SetAnyEventCallback(const AnyEventCallback&cb) { _event_callback = cb; }
        void SetSrvClosedCallback(const ClosedCallback&cb) { _server_closed_callback = cb; }
        void Established() {
            _loop->RunInLoop(std::bind(&Connection::EstablishedInLoop, this));
        }
        void Send(const char *data, size_t len) {
            Buffer buf;
            buf.WriteAndPush(data, len);
            _loop->RunInLoop(std::bind(&Connection::SendInLoop, this, std::move(buf)));
        }
        void Shutdown() {
            _loop->RunInLoop(std::bind(&Connection::ShutdownInLoop, this));
        }
        void Release() {
            _loop->QueueInLoop(std::bind(&Connection::ReleaseInLoop, this));
        }
        void EnableInactiveRelease(int sec) {
            _loop->RunInLoop(std::bind(&Connection::EnableInactiveReleaseInLoop, this, sec));
        }
        void CancelInactiveRelease() {
            _loop->RunInLoop(std::bind(&Connection::CancelInactiveReleaseInLoop, this));
        }
        void Upgrade(const Any &context, const ConnectedCallback &conn, const MessageCallback &msg, 
                     const ClosedCallback &closed, const AnyEventCallback &event) {
            _loop->AssertInLoop();
            _loop->RunInLoop(std::bind(&Connection::UpgradeInLoop, this, context, conn, msg, closed, event));
        }
};

// Acceptor 类，用于管理新连接的接受
class Acceptor {
    private:
        Socket _socket; // 监听套接字
        EventLoop *_loop; // 关联的 EventLoop
        Channel _channel; // 事件 Channel

        using AcceptCallback = std::function<void(int)>;
        AcceptCallback _accept_callback; // 接受回调
    private:
        void HandleRead() {
            int newfd = _socket.Accept();
            if (newfd < 0) {
                return ;
            }
            if (_accept_callback) _accept_callback(newfd);
        }
        int CreateServer(int port) {
            bool ret = _socket.CreateServer(port);
            assert(ret == true);
            return _socket.Fd();
        }
    public:
        Acceptor(EventLoop *loop, int port): _socket(CreateServer(port)), _loop(loop), 
            _channel(loop, _socket.Fd()) {
            _channel.SetReadCallback(std::bind(&Acceptor::HandleRead, this));
        }
        void SetAcceptCallback(const AcceptCallback &cb) { _accept_callback = cb; }
        void Listen() { _channel.EnableRead(); }
};

// TcpServer 类，用于管理 TCP 服务器
class TcpServer {
    private:
        uint64_t _next_id; // 自动增长的连接 ID
        int _port; // 端口
        int _timeout; // 非活跃连接的超时时间
        bool _enable_inactive_release; // 是否启用非活跃连接超时销毁
        EventLoop _baseloop; // 主线程的 EventLoop
        Acceptor _acceptor; // 监听套接字管理
        LoopThreadPool _pool; // 从属 EventLoop 线程池
        std::unordered_map<uint64_t, PtrConnection> _conns; // 连接管理

        using ConnectedCallback = std::function<void(const PtrConnection&)>;
        using MessageCallback = std::function<void(const PtrConnection&, Buffer *)>;
        using ClosedCallback = std::function<void(const PtrConnection&)>;
        using AnyEventCallback = std::function<void(const PtrConnection&)>;
        using Functor = std::function<void()>;
        ConnectedCallback _connected_callback; // 连接成功回调
        MessageCallback _message_callback; // 消息回调
        ClosedCallback _closed_callback; // 关闭回调
        AnyEventCallback _event_callback; // 任意事件回调
    private:
        void RunAfterInLoop(const Functor &task, int delay) {
            _next_id++;
            _baseloop.TimerAdd(_next_id, delay, task);
        }
        void NewConnection(int fd) {
            _next_id++;
            PtrConnection conn(new Connection(_pool.NextLoop(), _next_id, fd));
            conn->SetMessageCallback(_message_callback);
            conn->SetClosedCallback(_closed_callback);
            conn->SetConnectedCallback(_connected_callback);
            conn->SetAnyEventCallback(_event_callback);
            conn->SetSrvClosedCallback(std::bind(&TcpServer::RemoveConnection, this, std::placeholders::_1));
            if (_enable_inactive_release) conn->EnableInactiveRelease(_timeout);
            conn->Established();
            _conns.insert(std::make_pair(_next_id, conn));
        }
        void RemoveConnectionInLoop(const PtrConnection &conn) {
            int id = conn->Id();
            auto it = _conns.find(id);
            if (it != _conns.end()) {
                _conns.erase(it);
            }
        }
        void RemoveConnection(const PtrConnection &conn) {
            _baseloop.RunInLoop(std::bind(&TcpServer::RemoveConnectionInLoop, this, conn));
        }
    public:
        TcpServer(int port):
            _port(port), 
            _next_id(0), 
            _enable_inactive_release(false), 
            _acceptor(&_baseloop, port),
            _pool(&_baseloop) {
            _acceptor.SetAcceptCallback(std::bind(&TcpServer::NewConnection, this, std::placeholders::_1));
            _acceptor.Listen();
        }
        void SetThreadCount(int count) { return _pool.SetThreadCount(count); }
        void SetConnectedCallback(const ConnectedCallback&cb) { _connected_callback = cb; }
        void SetMessageCallback(const MessageCallback&cb) { _message_callback = cb; }
        void SetClosedCallback(const ClosedCallback&cb) { _closed_callback = cb; }
        void SetAnyEventCallback(const AnyEventCallback&cb) { _event_callback = cb; }
        void EnableInactiveRelease(int timeout) { _timeout = timeout; _enable_inactive_release = true; }
        void RunAfter(const Functor &task, int delay) {
            _baseloop.RunInLoop(std::bind(&TcpServer::RunAfterInLoop, this, task, delay));
        }
        void Start() { _pool.Create();  _baseloop.Start(); }
};

void Channel::Remove() { return _loop->RemoveEvent(this); }
void Channel::Update() { return _loop->UpdateEvent(this); }
void TimerWheel::TimerAdd(uint64_t id, uint32_t delay, const TaskFunc &cb) {
    _loop->RunInLoop(std::bind(&TimerWheel::TimerAddInLoop, this, id, delay, cb));
}
void TimerWheel::TimerRefresh(uint64_t id) {
    _loop->RunInLoop(std::bind(&TimerWheel::TimerRefreshInLoop, this, id));
}
void TimerWheel::TimerCancel(uint64_t id) {
    _loop->RunInLoop(std::bind(&TimerWheel::TimerCancelInLoop, this, id));
}

// NetWork 类，用于初始化网络设置
class NetWork {
    public:
        NetWork() {
            DBG_LOG("SIGPIPE INIT");
            signal(SIGPIPE, SIG_IGN);
        }
};
static NetWork nw;

#endif
